using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI;

namespace ModWatch
{

	/// <summary>
	/// Provides an implementation of <see cref="ModWatch.AlertBehaviorBase"/>
	/// that will execute a javascript function if any changes have been detected.
	/// </summary>
	public class CustomAlertBehavior : AlertBehaviorBase
	{

		#region Properties

		/// <summary>
		/// Gets or sets the javascript function to be executed
		/// if a change has been detected.
		/// </summary>
		[DefaultValue("")]
		public string ClientNotifyFunction
		{
			get { return GetProperty("ClientNotifyFunction", ""); }
			set { SetProperty("ClientNotifyFunction", value); }
		}

		#endregion

		/// <summary>
		/// Raises the <see cref="System.Web.UI.Control.Init"/> event.
		/// </summary>
		/// <param name="e">
		/// An <see cref="System.EventArgs"/>
		/// object that contains event data.
		/// </param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			this.GettingScriptReferences += new EventHandler<ScriptReferencesEventArgs>(this_GettingScriptReferences);
		}

		/// <summary>
		/// Registers the scripts needed by the
		/// <see cref="ModWatch.CustomAlertBehavior"/> class.
		/// </summary>
		/// <param name="sender">
		/// The <see cref="System.Object"/> that fired the
		/// <see cref="ModWatch.ModWatchScriptControlBase.GettingScriptReferences"/> event.
		/// </param>
		/// <param name="e">
		/// A <see cref="ModWatch.ScriptReferencesEventArgs"/> object that contains the event data.
		/// </param>
		private void this_GettingScriptReferences(object sender, ScriptReferencesEventArgs e)
		{
			e.AddWebResourceScriptReference("ModWatch.Scripts.CustomAlert.js");
			e.AddWebResourceScriptReference("ModWatch.Scripts.CustomAlertEventArgs.js");
		}

		/// <summary>
		/// Registers the <see cref="System.Web.UI.ScriptDescriptor"/>
		/// objects for the control and returns an object that contains
		/// the <see cref="System.Web.UI.ScriptDescriptor"/> objects for the control.
		/// </summary>
		/// <returns></returns>
		public override IEnumerable<ScriptDescriptor> GetScriptDescriptors()
		{
			ScriptComponentDescriptor sd = new ScriptComponentDescriptor("ModWatch.CustomAlert");
			sd.ID = this.ClientID;
			sd.AddScriptProperty("clientNotifyFunction", this.ClientNotifyFunction);

			return new ScriptDescriptor[] { sd };
		}

	}

}
